<?php
/**
 * @version     $Id$
 * @author      Luu Trong Hieu <tronghieu1012@yahoo.com>
 * @license 	GNU/GPL see license.php
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 */

defined('IN_FLYWHEEL') or die('Restricted Access');
require_once _CONTROLLERS_PATH_.'Backend.controller.php';

/**
 * Profiles Controller
 *
 * @package     backend
 * @subpackage  profiles
 * 
 */
class ProfilesController extends BackendController {
    public function executeDefault() {    	
        fFactory::getI18N()->getLanguageSource()->import('user', false);
        $template = fFactory::getTemplate();
        $profiles = Doctrine_Core::getTable('User')->findOneById(fFactory::getUser()->getId());

    	if (fFactory::getRequest()->getMethod() == fRequest::POST) { //submit
        	$this->_save($profiles);        	        	
        }
        
        $template->assign('profiles', $profiles);
        $birthdayArr = explode('-', $profiles->birthday);
        $birthday = new fDate();
        $birthday['year'] = $birthdayArr[0];
        $birthday['mon'] = $birthdayArr[1];
        $birthday['mday'] = $birthdayArr[2];
        
        fFactory::getUser()->setCulture('en-US');
        $birthdaySelect = new fFormElementSelectDate('birthday', $birthday);
        $birthdaySelect->setDisplayMounthMode(fFormElementSelectDate::DISPLAY_MOUNTH_ABBREVIATED);
        $birthdaySelect->setOption('start_year', 1901);
        $template->assign('birthday_select_tags', $birthdaySelect->render());
        $template->assign('action', fUri::createUrl('profiles'));
    }
    
    private function _save(User &$profile) {
    	$request = fFactory::getRequest();
    	$message = new fMessage();
    	    	
    	$profile->last_name = $request->get('last_name');    	
    	$profile->first_name = $request->get('first_name');
    	$birthday = $request->get('birthday', fFilter::TYPE_ARRAY);
    	$profile->birthday = $birthday['year'] .'-' .$birthday['mounth'] .'-' .$birthday['day'];
    	$profile->gender = $request->get('gender');
    	$profile->email = $email = $request->get('email');
    	
    	if ($profile->last_name == '') {
    		$message->add(_('last name is required!'), _('Last name'), fMessage::TYPE_ERROR);
    	}
    	
    	if ($profile->first_name == '') {
    		$message->add(_('first name is required!'), _('First name'), fMessage::TYPE_ERROR);
    	}
    	
    	if ($email != '') {
    		$validatorEmail = new fValidatorEmail();
    		if (!$validatorEmail->validate($email)) {
    			$message->add(implode('', $validatorEmail->getFailuresMessage()), _('Email'), fMessage::TYPE_ERROR);    			    		
    		}
    		else if (false) {// check duplicate email
    			$message->add(_('email already use!'), _('Email'), fMessage::TYPE_ERROR);    			
    		}
    	}
    	
    	if ($request->get('new_password') != null) { // Change pass
    		$currentPassword = explode(':', $profile->password);
    		if (md5($request->get('current_password').$currentPassword[1]) != $currentPassword[0]) {
    			//current password input not match
    			$message->add(_('current password not match!', _('Password', fMessage::TYPE_ERROR)));
    		}
    		else if (($newpass = $request->get('new_password')) != $request->get('retype_password')) {
    			//Retype password not match
    			$message->add(_('retype password not match'), _('Password'), fMessage::TYPE_ERROR);    			    			    			
    		}
    		else {
    			$validatorString = new fValidatorString(array('max_length'=>30, array('min_length'=> 5)));
    			if ($validatorString->validate($newpass)) {    			
    				$profile->password = md5($newpass . fUser::createSalt());
    			}
    			else {
    				$message->add(implode('', $validatorString->getFailuresMessage())); 			    			    			
    			}
    		}
    	}
    	
    	if ($message->check(fMessage::TYPE_ERROR)) {
	    	try {
	    		$profile->save();
	    		$message->add(_('Profiles updated'), _('Success'));	    		    	    	
	    	}
	    	catch (Doctrine_Exception $de) {
	    		$message->add($de->getMessage(), 'Doctrine Error', fMessage::TYPE_ERROR);	    		
	    	}    		
    	}
    }
}
